﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Utilities;
using Telerik.Web.Mvc;
using BLL;

namespace Grant_Warehouse.Controllers
{ 
    public class StockChangeRecordController : Controller
    {
        public ViewResult Index()
        {
            return View(CH.GetAllData<StockChangeRecord>());
        }

        [GridAction]
        public ViewResult AjaxStockChangeRecordIndex()
        {
            return View(CT.GetViewModel<StockChangeRecord, StockChangeRecordView>());
        }

        public ViewResult Details(int id)
        {
            return View(CH.GetDataById<StockChangeRecord>(id));
        }

        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">stockid</param>
        /// <returns></returns>
        public ActionResult StockReduce(int id)
        {
            var s = CH.GetDataById<Stock>(id); 
            var change = StockManagement.GetStockChangeInstance(s);
            ViewBag.MaxVolume = s.Volume.ToString();
            return View(change);
        }

        [HttpPost]
        public ActionResult StockReduce(StockChangeRecord s)
        {
            if (ModelState.IsValid)
            {
                string msg;
                if (StockManagement.StocksChange(s, out msg))
                {
                    CH.Create<StockChangeRecord>(s);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, msg);
                    return View(s);
                }
                return RedirectToAction("Index");
            }
            return View(s);
        } 

        [HttpPost]
        public ActionResult Create(StockChangeRecord s)
        {
            if(ModelState.IsValid)
            {
                string msg;
                if (StockManagement.StocksChange(s,out msg))
                {
                    CH.Create<StockChangeRecord>(s);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, msg);
                }
                return RedirectToAction("Index");
            }
            return View(s);
        }
 
        public ActionResult Edit(int id)
        {
            StockChangeRecord s = CH.GetDataById<StockChangeRecord>(id);
            return View(s);
        }

        [HttpPost]
        public ActionResult Edit(StockChangeRecord stockchangerecord)
        {
            if (ModelState.IsValid)
            {
                CH.Edit<StockChangeRecord>(stockchangerecord);
                return RedirectToAction("Index");
            }
            return View(stockchangerecord);
        }
 
        public ActionResult Delete(int id)
        {
            StockChangeRecord stockchangerecord = CH.GetDataById<StockChangeRecord>(id); 
            return View(stockchangerecord);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                CH.Delete<StockChangeRecord>(id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "此数据被其他数据引用，为保证数据完整性，请先移除对此数据的引用，再进行删除");
                Log.LogError(ex, this);
                return View(CH.GetDataById<StockChangeRecord>(id));
            }
            return RedirectToAction("Index");
        }
    }
}